// pages/modifyPSW/modifyPSW.js
const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldpwd: '',
    newpwd: '',
    confirmpwd: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  formSubmit: util.throttle(function(e) {
    let oldpwd = e.detail.value.oldpwd
    let newpwd = e.detail.value.newpwd
    let confirmpwd = e.detail.value.confirmpwd
    if (oldpwd == '' || app.trim(newpwd) == '' || app.trim(confirmpwd) == '') {
      wx.showModal({
        content: "密码不可为空或空格，请重新输入",
        showCancel: false,
        success: res => {
          if (res.confirm) {}
        }
      })
    } else if (oldpwd == newpwd) {
      wx.showModal({
        content: "新密码与旧密码相同，请重新输入新密码",
        showCancel: false,
        success: res => {
          if (res.confirm) {
            this.setData({
              newpwd: '',
              confirmpwd: ''
            })
          }
        }
      })
    } else if (newpwd != confirmpwd) {
      wx.showModal({
        content: "两次输入新密码不一致，请重新输入",
        showCancel: false,
        success: res => {
          if (res.confirm) {
            this.setData({
              newpwd: '',
              confirmpwd: ''
            })
          }
        }
      })
    } else {
      wx.request({
        url: app.userUrl + '/WeChat/AccountManagement/updateUserPassWord',
        method: 'POST',
        data: {
          username: app.globalData.username,
          oldpwd: oldpwd,
          newpwd: newpwd
        },
        dataType: 'json',
        success: res => {
          if (res.data.res == "success") {
            wx.showModal({
              content: res.data.msg + "，点击确定后重新登录",
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  // console.log(res)
                  // 跳转到 重新登录
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  this.setData({
                    oldpwd: '',
                    newpwd: '',
                    confirmpwd: ''
                  })
                }
              }
            })
          }
        },
        fail: res => {
          wx.showToast({
            title: '网络异常！',
            image: '/images/warning.png'
          })
        }
      })
    }
  })
})